<template>
  <div>
    <!-- 大客户订单明细查询-->
    <TopFilter ref="topform" :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>
    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTables
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        columnsRef="bigCustomerOrderDetailQuery"
        :isSelectColumn="false"
        :onColumnsChange="(columns) => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle()">导 出</multiuse-button>
        </template>
      </FilterTables>
    </Spin>
    <Drawer :visible.sync="detailVisible" :title="'大客户订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <detailModel :rowData="formData" @close="detailcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { getLevelOneArea } from '@/api/cms-common/dealerdata/index';
import { region } from '@/api/cms-common/region';
import { getBrandListOEM, getSeriesListOEM, getModelListOEM, getPackageListOEM, getOptionListDictOEM, getWcolorList, getIcolorList } from '@/api/common/cars/masterDataCars';
import { getList, vehicleConfiguration, exportList, exporturl } from '@/api/omSaleP/KeyCustomers/OrderQuery/index.js';
import { exportExcelFile } from '@/utils';
import detailModel from './detailModel';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'bigCustomerOrderDetailQuery',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        isDealer: 10041002,
      },
      fetchApi: getList,
      formData: {},
      exportLoading: false,
      detailVisible: false,
    };
  },
  components: {
    detailModel,
  },
  mixins: [dictionary, region],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    //初始化所属区域数据
    this.getTopLevelArea();
    //初始化省份数据
    this.initProvince();
  },
  created() {
    // //获取产品相关数据
    // this.getvehicleList();
    // //获取经销商资金类型
    // this.gettmFiAccountType();
    //初始化品牌数据
    this.getDictBrandList();
    //初始化外饰颜色数据
    this.getColors();
    //初始化内饰颜色数据
    this.getIColors();
  },
  methods: {
    // 所属区域
    async getTopLevelArea() {
      const res = await getLevelOneArea(15061015);
      if (res.resultCode === 200) {
        this.topFilterList.find((item) => item.fieldName === 'areaId').itemList = res.data.map((x) => ({ value: parseInt(x.id), text: x.orgName }));
      }
    },
    //初始化所在省份
    async initProvince() {
      let pl = await this.createRegionList();
      this.topFilterList.find((item) => item.fieldName === 'provinceId').itemList = pl;
    },
    //初始化所在市数据
    async initCity(provinceId) {
      this.topFilterList.find((item) => item.fieldName == 'cityId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName == 'cityId').itemList = [];
      if (!provinceId || provinceId == null || typeof provinceId == 'undefined') {
        return;
      }
      //取省数据
      let rd = this.getRegionData();
      this.cityData = rd[provinceId].children;
      let cl = this.createSubRegionList(this.cityData);
      //初始化市，清除原值
      let city = this.topFilterList.find((item) => item.fieldName === 'cityId');
      city.itemList = cl;
      city.initialValue = '';
    },
    //获取品牌信息
    async getDictBrandList() {
      const res = await getBrandListOEM();
      if (res.resultCode === 200) {
        // console.log(this.topFilterList, 'this.topFilterList');
        this.topFilterList.find((item) => item.fieldName === 'brandId').itemList = res.data.map((x) => ({ value: x.brandId, text: x.brandName }));
      }
    },
    //获取内饰颜色信息
    async getColors() {
      const res = await getWcolorList();
      if (res.resultCode === 200) {
        this.topFilterList.find((item) => item.fieldName === 'colorId').itemList = res.data.map((x) => ({ value: x.colorId, text: x.colorName }));
      }
    },
    //获取外饰颜色信息
    async getIColors() {
      const res = await getIcolorList();
      if (res.resultCode === 200) {
        this.topFilterList.find((item) => item.fieldName === 'trimColorId').itemList = res.data.map((x) => ({ value: x.colorId, text: x.colorName }));
      }
    },
    //修改品牌下拉列表
    async changeBrandSelect(brandsid) {
      this.topFilterList.find((item) => item.fieldName === 'seriesId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'seriesId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'modelId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'modelId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'packageId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
      if (brandsid) {
        const res = await getSeriesListOEM(brandsid);
        if (res.resultCode === 200) {
          this.topFilterList.find((item) => item.fieldName === 'seriesId').itemList = res.data.map((x) => ({ value: x.seriesId, text: x.seriesName }));
        }
      }
    },
    //修改车系下拉列表
    async changeSeriesSelect(seriessid) {
      const brandsid = this.topFilterList.find((item) => item.fieldName === 'brandId').initialValue;
      if (brandsid && seriessid) {
        this.topFilterList.find((item) => item.fieldName === 'modelId').initialValue = '';
        this.topFilterList.find((item) => item.fieldName === 'modelId').itemList = [];
        this.topFilterList.find((item) => item.fieldName === 'packageId').initialValue = '';
        this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = [];
        this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
        this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
        const res = await getModelListOEM(brandsid, seriessid);
        if (res.resultCode === 200) {
          this.topFilterList.find((item) => item.fieldName === 'modelId').itemList = res.data.map((x) => ({ value: x.modelId, text: x.modelName }));
        }
      }
    },
    //修改车型下拉列表
    async changeModelSelect(modelId) {
      const brandsid = this.topFilterList.find((item) => item.fieldName === 'brandId').initialValue;
      const seriessid = this.topFilterList.find((item) => item.fieldName === 'seriesId').initialValue;
      this.topFilterList.find((item) => item.fieldName === 'packageId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
      if (modelId) {
        const res = await getPackageListOEM(brandsid, seriessid, modelId);
        if (res.resultCode === 200) {
          this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = res.data.map((x) => ({ value: x.packageId, text: x.configName }));
        }
      }
    },
    // 修改配置下拉列表
    async changePackageSelect(packageId) {
      const brandsid = this.topFilterList.find((item) => item.fieldName === 'brandId').initialValue;
      const seriessid = this.topFilterList.find((item) => item.fieldName === 'seriesId').initialValue;
      this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
      if (packageId) {
        const res = await getOptionListDictOEM(packageId);
        if (res.resultCode === 200) {
          this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = res.data.map((x) => ({ value: x.optionId, text: x.optionName }));
        }
      }
    },
    // 获取车型车系
    async getvehicleList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find((item) => item.fieldName === 'brandProductList').itemList = res.data;
    },
    // // 获取资金类型
    // async gettmFiAccountType() {
    //   let res = (await gettmFiAccountType({ fundsType: 0 })).data.map((x) => ({ text: x.accountTypeName, value: x.id }));
    //   this.topFilterList.find((x) => x.fieldName == 'accountType').itemList = res;
    // },
    //导出
    async addHandle() {
      // let json = Object.assign({}, { CODE: 'CODE_0000011' }, this.fetchParams);
      // let res = await exporturl(json);

      // if (res.resultCode == 200) {
      //   this.$router.push('/admin/auth/oem/downloadCenter');
      // }

      this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$refs.topform.SUBMIT_FORM();
          let json = this.fetchParams;
          // this.exportLoading = true;
          exportList(json)
            .then((response) => {
              exportExcelFile(response.data, '大客户订单明细查询');
              // this.exportLoading = false;
            })
            .catch((err) => {
              // console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          });
        });
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.formData = data;
      this.detailVisible = true;
    },
    detailcloseHandler() {
      this.detailVisible = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    resetChange(params) {
      //清空下拉列表数据
      this.topFilterList.find((item) => item.fieldName === 'seriesId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'seriesId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'modelId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'modelId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'packageId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    createdFrom() {
      return [
        {
          type: 'TEXT_AREA',
          label: '结算订单编号',
          fieldName: 'doNo',
          maxlength: '10000',
          placeholder: '输入多个结算订单编号时，请用英文逗号或者换行隔开（例如：单号1,单号2……）',
          maxRows: 2,
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          maxlength: '10000',
          placeholder: '输入多个VIN时，请用英文逗号或者换行隔开（例如：VIN1,VIN2……）',
          maxRows: 2,
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: this.createDictList('2105').filter((item) => item.value == '21051004' || item.value == '21051005' || item.value == '21051006'),
        },
        {
          type: 'INPUT',
          label: '订单编号',
          fieldName: 'orderNo',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '订单明细状态',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          itemList: this.createDictList('2109'),
        },
        {
          type: 'SELECT',
          label: '车辆用途',
          fieldName: 'vehicleUse',
          placeholder: '请选择',
          itemList: this.createDictList('2102'),
        },

        // {
        //   type: 'INPUT_CASCADER',
        //   label: '车型选择',
        //   fieldName: 'vehicleInfo',
        //   placeholder: '请选择',
        //   options: { titles: ['车系', '车型', '配置', '选装', '颜色'] },
        //   itemList: []
        // },
        {
          type: 'SELECT',
          label: '所属区域',
          fieldName: 'areaId',
          placeholder: '请选择',
          itemList: [],
        },
        {
          type: 'SELECT',
          label: '省份',
          fieldName: 'provinceId',
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
          change: (props) => this.initCity(props),
        },
        {
          type: 'SELECT',
          label: '城市',
          fieldName: 'cityId',
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '输入多个经销商代码时，请用英文逗号隔开',
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brandId',
          placeholder: '请选择',
          itemList: [],
          change: (val) => {
            this.changeBrandSelect(val);
          },
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'seriesId',
          placeholder: '请选择',
          itemList: [],
          change: (val) => {
            this.changeSeriesSelect(val);
          },
        },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'modelId',
          placeholder: '请选择',
          itemList: [],
          change: (val) => {
            this.changeModelSelect(val);
          },
        },
        {
          type: 'SELECT',
          label: '配置',
          fieldName: 'packageId',
          placeholder: '请选择',
          itemList: [],
          change: (val) => {
            this.changePackageSelect(val);
          },
        },
        {
          type: 'SELECT',
          label: '选装',
          fieldName: 'optionId',
          placeholder: '请选择',
          itemList: [],
        },
        {
          type: 'SELECT',
          label: '外饰色',
          fieldName: 'colorId',
          placeholder: '请选择',
          itemList: [],
        },
        {
          type: 'SELECT',
          label: '内饰色',
          fieldName: 'trimColorId',
          placeholder: '请选择',
          itemList: [],
        },
        {
          type: 'INPUT',
          label: '物料编码',
          fieldName: 'productCode',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '物料描述',
          fieldName: 'productName',
          placeholder: '请输入',
        },
        {
          type: 'RANGE_DATE',
          label: '提报时间',
          fieldName: 'reportDateBegin|reportDateEnd',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange',
        },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  详情
                </multiuse-button>
              </div>
            );
          },
        },
        {
          title: '所属区域',
          dataIndex: 'areaName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '省份',
          dataIndex: 'provinceName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '城市',
          dataIndex: 'cityName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '大客户来源',
          dataIndex: 'customersType',
          sorter: true,
          filter: true,
          render: (p) => {
            return <span>{this.createDictText(p.row.customersType, '2108')}</span>;
          },
        },
        {
          title: '大客户编号',
          dataIndex: 'customersNo',
          sorter: true,
          filter: true,
          width: 200,
          filterType: 'input',
        },
        {
          title: '订单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 200,
        },
        {
          title: '结算订单编号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 200,
        },
        {
          title: '订单年月',
          dataIndex: 'orderYearMonth',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '紧急度',
          dataIndex: 'emergencyType',
          sorter: true,
          filter: true,
          render: (p) => {
            return <span>{this.createDictText(p.row.emergencyType, '2101')}</span>;
          },
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'intput',
          render: (p) => {
            return <span>{this.createDictText(p.row.orderType, '2105')}</span>;
          },
        },
        {
          title: '订单细分类型',
          dataIndex: 'orderDetailTypeName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '车辆用途',
          dataIndex: 'vehicleUse',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span>{this.createDictText(p.row.vehicleUse, '2102')}</span>;
          },
        },
        {
          title: '订单明细状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span>{this.createDictText(p.row.orderStatus, '2109')}</span>;
          },
        },
        {
          title: '资金类型',
          dataIndex: 'accountTypeName',
          sorter: true,
          filter: true,
          width: 150,
          filterType: 'input',
        },
        {
          title: '市场指导价',
          dataIndex: 'singleAmount',
          sorter: true,
          filter: true,
          precision: 2,
          numberFormat: true,
          align: 'right',
          filterType: 'input',
        },
        {
          title: '价格类型',
          dataIndex: 'amountType',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span>{this.createDictText(p.row.amountType, '2136')}</span>;
          },
        },
        {
          title: '采购价格',
          dataIndex: 'afterDiscountAmount',
          sorter: true,
          filter: true,
          precision: 2,
          numberFormat: true,
          align: 'right',
          filterType: 'input',
        },
        {
          title: '现金折让',
          dataIndex: 'discountAmount',
          sorter: true,
          filter: true,
          precision: 2,
          numberFormat: true,
          align: 'right',
          filterType: 'input',
        },
        {
          title: '建店返利',
          dataIndex: 'buildRebateAmount',
          sorter: true,
          filter: true,
          precision: 2,
          numberFormat: true,
          align: 'right',
          filterType: 'input',
        },
        {
          title: '是否使用返利',
          dataIndex: 'isRebate',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span>{this.createDictText(p.row.isRebate, '1004')}</span>;
          },
        },
        {
          title: '返利冲抵',
          dataIndex: 'singleRebateAmount',
          sorter: true,
          filter: true,
          precision: 2,
          numberFormat: true,
          align: 'right',
          filterType: 'input',
        },
        {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span>{this.createDictText(p.row.transportType, '2409')}</span>;
          },
        },
        {
          title: '收车地址',
          dataIndex: 'address',
          sorter: true,
          filter: true,
          width: 200,
          filterType: 'input',
        },
        {
          title: '送达城市',
          dataIndex: 'orderCity',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '联系电话',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '物料编码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          width: 200,
          filterType: 'input',
        },
        {
          title: '物料描述',
          dataIndex: 'productName',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '品牌',
          dataIndex: 'brandName',
          sorter: true,
          filter: true,
          width: 150,
          filterType: 'input',
        },
        {
          title: '车系',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          width: 150,
          filterType: 'input',
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          width: 150,
          filterType: 'input',
        },
        {
          title: '配置',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          width: 150,
          filterType: 'input',
        },
        {
          title: '选装',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '提报时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
        },
        {
          title: '业务审核时间',
          dataIndex: 'checkDate',
          sorter: true,
          filter: true,
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
        },
        {
          title: '承兑汇票号',
          dataIndex: 'billNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '财务审核时间',
          dataIndex: 'financeCheckDate',
          sorter: true,
          filter: true,
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
        },
        {
          title: '出库时间',
          dataIndex: 'sapOutDate',
          sorter: true,
          filter: true,
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
        },
        {
          title: '发运时间',
          dataIndex: 'commandDate',
          sorter: true,
          filter: true,
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
        },
      ];
    },
  },
};
</script>
